import java.util.concurrent.CountDownLatch;

/**
 * 3（2）懒汉式（线程安全，但效率低）
 *
 * @author agstar
 * @date 2019/7/3 10:21
 */
public class LazySynchronized {

    private static LazySynchronized instance = null;

    /**
     * 构造函数私有化
     */
    private LazySynchronized() {
    }

    /**
     * 提供一个全局的静态方法
     */
    public static synchronized LazySynchronized getInstance() {
        if (instance == null) {
            instance = new LazySynchronized();
        }
        return instance;
    }

    public static void main(String[] args) {
        int count = 2000;
        CountDownLatch latch = new CountDownLatch(count);

        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LazySynchronized lazytwo = LazySynchronized.getInstance();
                System.out.println(lazytwo);
            }).start();
            latch.countDown();
        }
    }

}
